import { createStore } from '@/airstore';
import ApiClient from '@/services/apiClient';
import { formatDate } from '@/helpers/dateutil';

export default createStore({
  namespace: 'FlightSchedule',
  filterCond: {
    startDate: null,
    endDate: null,
    selAirport: 'HGH',
    selAirplane: '',
  },
  list: [[], [], [], []],
  setFlightScheduleList(scData, filter) {
    if (scData) {
      const totalList = [];
      const unflyList = [];
      const hasflyList = [];
      const hasarrivelList = [];
      for (let i = 0; i < scData.length; i++) {
        const scDataItem = scData[i];
        const schedule = {
          id: i,
          airplane_no: scDataItem.VF_FlightID,
          embarkedBay: scDataItem.VF_Bay,
          arrivedBay: scDataItem.VF_Bay2,
        };
        //实际起飞时间
        const atdTime = formatDate(scDataItem.VF_ATD, 'YYYY-MM-DD HH:mm:ss');
        //实际到达时间
        const ataTime = formatDate(scDataItem.VF_ATA, 'YYYY-MM-DD HH:mm:ss');
        //预计起飞时间
        const stdTime = formatDate(scDataItem.VF_STD, 'YYYY-MM-DD HH:mm:ss');
        //预计到达时间
        const staTime = formatDate(scDataItem.VF_STA, 'YYYY-MM-DD HH:mm:ss');
        //全部 未飞  起飞 到达
        if (atdTime === '') {
          schedule.departureTime = stdTime;
          schedule.arrivelTime = staTime;
          unflyList.push(schedule);
        } else if (atdTime !== '') {
          schedule.departureTime = atdTime;
          schedule.arrivelTime = ataTime || staTime;
          hasflyList.push(schedule);
        } else if (ataTime !== '' && atdTime !== '') {
          schedule.departureTime = atdTime;
          schedule.arrivelTime = ataTime;
          hasarrivelList.push(schedule);
        }
        totalList.push(schedule);
      }
      this.list = [totalList, unflyList, hasflyList, hasarrivelList];
    } else {
      this.list = [[], [], [], []];
    }
    this.filterCond = filter;
  },

  async getFlightScheduleList(loginName, filter) {
    const searchPara = {
      datefrom: filter.startDate.format('YYYY-MM-DD'),
      dateto: filter.endDate.format('YYYY-MM-DD'),
      stationid: filter.selAirport,
      acno: filter.selAirplane,
    };
    if (filter.endDate == null || filter.startDate == null) {
      return;
    } else {
      // console.log('getr flight', searchPara);{
      const srRes = await ApiClient.post('zoController/v1/getFlightData', {
        loginUser: loginName,
        jsonResult: JSON.stringify(searchPara),
      });
      // console.log('get flight schedule', filter, srRes);
      this.setFlightScheduleList(!srRes.errorCode ? srRes.data : null, filter);
      return srRes;
    }
  },
});
